package coop.bancocredicoop.proyectos.gd;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import coop.bancocredicoop.proyectos.gd.domain.DocumentType;

public class DocumentTypeForm {

	private Long daysInAdvanceForNotice;
	private Date defaultExpirationDay;
	private Long defaultLastingMonths;
	private boolean hasExpiration;
	private Long id;
	private String name;
	private boolean purgeable;
	private String shortName;
	private String uploadNotificationEmailAddress;
	private Long version;
	private boolean versionable;
	private DocumentType sourceDocumentType;

	public DocumentTypeForm(DocumentType sourceDocumentType) {
		if (sourceDocumentType == null) {
			initWithNullDocumentType();
		} else {
			initWithDocumentType(sourceDocumentType);
		}
	}
	
	public DocumentTypeForm() {
		initWithNullDocumentType();
	}
	
	private void initWithDocumentType(DocumentType documentType) {
		daysInAdvanceForNotice = documentType.getDaysInAdvanceForNotice();
		defaultExpirationDay = documentType.getDefaultExpirationDay();
		defaultLastingMonths = documentType.getDefaultLastingMonths();
		hasExpiration = documentType.getHasExpiration();
		id = documentType.getId();
		name = documentType.getName();
		purgeable = documentType.getPurgeable();
		shortName = documentType.getShortName();
		uploadNotificationEmailAddress = documentType.getUploadNotificationEmailAddress();
		version = documentType.getVersion();
		versionable = documentType.getVersionable();
	}

	private void initWithNullDocumentType() {
		version = 0L;
		hasExpiration = false;
		defaultLastingMonths = 12L;
		daysInAdvanceForNotice = 7L;
	}

	public String getName() {
		return name;
	}

	public String getShortName() {
		return shortName;
	}

	public String getUploadNotificationEmailAddress() {
		return uploadNotificationEmailAddress;
	}

	public boolean getVersionable() {
		return versionable;
	}

	public Long getVersion() {
		return version;
	}

	public boolean getPurgeable() {
		return purgeable;
	}

	public Long getDaysInAdvanceForNotice() {
		return daysInAdvanceForNotice;
	}

	public Date getDefaultExpirationDay() {
		return defaultExpirationDay;
	}

	public Long getDefaultLastingMonths() {
		return defaultLastingMonths;
	}

	public boolean getHasExpiration() {
		return hasExpiration;
	}

	public Long getId() {
		return id;
	}
	
	public void fillDocumentTypeWithParameters(DocumentType documentType, HttpServletRequest request) {
		
		Long daysInAdvanceForNotice;
		Long defaultLastingMonths;
		boolean hasExpiration;
		String name;
		boolean purgeable;
		String shortName;
		String uploadNotificationEmailAddress;
		Long version;
		boolean versionable;
		
		daysInAdvanceForNotice = ServletUtils.getParameterAsLong(request, "daysInAdvanceForNotice", getDaysInAdvanceForNotice());
		defaultLastingMonths = ServletUtils.getParameterAsLong(request, "defaultLastingMonths", getDefaultLastingMonths());
		hasExpiration = ServletUtils.getParameterAsBoolean(request, "hasExpiration", getHasExpiration());
		name = ServletUtils.getParameterAsString(request, "name", getName());
		purgeable = ServletUtils.getParameterAsBoolean(request, "purgeable", getPurgeable());
		shortName = ServletUtils.getParameterAsString(request, "shortName", getShortName());
		uploadNotificationEmailAddress = ServletUtils.getParameterAsString(request, "uploadNotificationEmailAddress", getUploadNotificationEmailAddress());
		version = ServletUtils.getParameterAsLong(request, "version", getVersion());
		versionable = ServletUtils.getParameterAsBoolean(request, "versionable", getVersionable());
		
		documentType.setDaysInAdvanceForNotice(daysInAdvanceForNotice);
		documentType.setDefaultLastingMonths(defaultLastingMonths);
		documentType.setHasExpiration(hasExpiration);
		documentType.setName(name);
		documentType.setPurgeable(purgeable);
		documentType.setShortName(shortName);
		documentType.setUploadNotificationEmailAddress(uploadNotificationEmailAddress);
		documentType.setVersion(version + 1);
		documentType.setVersionable(versionable);
		
	}


}
